<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Login extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        if (request()->isAjax()) {
            $data=input('post.');
            if(!captcha_check($data['code'])){
                return json(['code'=>2,'验证码错误']);
               };
            $name=db('manager')->where('uname',$data['uname'])->find();
            if (!$name) {
                return json(['code'=>2,'该用户不存在']);
            }
            if ($name['status']==0) {
                return json(['code'=>2,'该用户以锁定，请联系管理员']);
            }
            if ($name['password']!=md5($data['password'])) {
                return json(['code'=>2,'密码不正确，请重试']);
            }
            if ($name['password']==md5($data['password'])) {
                session('uname',$name['uname']);
                session('uid',$name['id']);
                db('manager')->where('id',$name['id'])->setField('lasttime',time());
                return json(['code'=>1,'登录成功']);
            }
        }
        return view();
    }
    public function logout(){
        session(null);
        $this->success('退出成功','admin/login/index');
    }

}
